package com.android.graffiti;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.util.ImageHelper;


/**
 * 在涂鸦界面用作画笔颜色、大小及形状选择的弹出框控件
 */
public class BrushesPopupView extends LinearLayout {
    
    private RadioGroup mColorsGroup;
    private RadioGroup mWidthGroup;

    private boolean mHasMosaic = true;

    public void setHasMosaic(boolean hasMosaic) {
        mHasMosaic = hasMosaic;
    }
    
    public BrushesPopupView(Context context) {
        super(context);
        initilize();
    }
    
    public BrushesPopupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initilize();
    }

    /**
     * 初始化
     */
    private void initilize() {
        LayoutInflater li = (LayoutInflater)getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.graffito_popup_brushes, this, true);
        
        mColorsGroup = (RadioGroup) findViewById(R.id.graffito_popup_brushes_colors_gp);
        mWidthGroup = (RadioGroup) findViewById(R.id.graffito_popup_brushes_width_gp);

    }

    public void init() {
        if(!mHasMosaic) {
            mColorsGroup.removeViewAt(0);
        }
        countFitMargin(mColorsGroup, 12);
        countFitMargin(mWidthGroup, 8);
    }

    private void countFitMargin(RadioGroup group, int margin){
        int count = group.getChildCount();
        int defaultSize = 32;
        if(count * DeviceUtils.dip2px(getContext(), defaultSize + margin * 2) <= ImageHelper.SCREEN_WIDTH){

        } else {
            float number = ImageHelper.SCREEN_WIDTH / DeviceUtils.dip2px(getContext(), defaultSize + margin * 2f);
            float mini = number - (int) number;
            if (mini >= 0.5f && mini <= 0.7f) {
            } else {
                int width;
                width = (int) (ImageHelper.SCREEN_WIDTH / ((int) number + 0.58f));

                int newMargin = (width - DeviceUtils.dip2px(getContext(), defaultSize)) / 2;
                for (int i = 0; i < count; i++) {
                    View v = group.getChildAt(i);
                    RadioGroup.LayoutParams lp = (RadioGroup.LayoutParams) v.getLayoutParams();
                    lp.leftMargin = newMargin;
                    lp.rightMargin = newMargin;
                    v.setLayoutParams(lp);
                }
            }
        }
    }
    
    /**
     * 获取指定序号的{@link RadioButton}
     * @param index 指定的序号
     */
    public RadioButton getRadioButton(RadioGroup rg, int index) {
        if(rg != null) {
            return (RadioButton) rg.getChildAt(index);
        } else {
            return null;
        }
    }
    
    
    /**
     * 设置颜色单选项改变的监听器
     * @param l 单选项改变的监听器
     */
    public void setColorCheckedListener(RadioGroup.OnCheckedChangeListener l) {
        if(mColorsGroup != null) {
            mColorsGroup.setOnCheckedChangeListener(l);
        }
    }
    
    /**
     * 设置粗细单选项改变的监听器
     * @param l 单选项改变的监听器
     */
    public void setWidthCheckedListener(RadioGroup.OnCheckedChangeListener l) {
        if(mWidthGroup != null) {
            mWidthGroup.setOnCheckedChangeListener(l);
        }
    }
    
    
    /**
     * 设置要选中的粗细
     * @param index 要选中的按钮的序号
     */
    public void setWidthButtonChecked(int index, boolean checked) {
        RadioButton button = getRadioButton(mWidthGroup, index);
        if(button != null) {
            button.setChecked(checked);
        }
    }
    
    /**
     * 获取选中的粗细的序号
     * @return 选中的粗细的序号
     */
    public int getCheckedWidthIndex() {
        return getCheckedIndex(mWidthGroup);
    }
    
    
    /**
     * 设置要选中的颜色
     * @param index 要选中的按钮的序号
     */
    public void setColorButtonChecked(int index, boolean checked) {
        RadioButton button = getRadioButton(mColorsGroup, index);
        if(button != null) {
            button.setChecked(checked);
        }
    }
    
    /**
     * 获取选中的颜色的序号
     * @return 选中的颜色的序号
     */
    public int getCheckedColorIndex() {
        return getCheckedIndex(mColorsGroup);
    }
    
    
    /**
     * 获取选中的按钮的序号
     * @return 选中的按钮的序号
     */
    protected int getCheckedIndex(RadioGroup rg) {
        if(rg != null) {
            int btnId = rg.getCheckedRadioButtonId();
            RadioButton button = (RadioButton) rg.findViewById(btnId);
            if(button != null) {
                return rg.indexOfChild(button);
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }
    
    
    /**
     * 显示该控件
     */
    public void show() {
        this.setVisibility(View.VISIBLE);
    }
    
    
    /**
     * 隐藏该控件
     */
    public void hide() {
        this.setVisibility(View.INVISIBLE);
    }
    
    
    /**
     * 尝试隐藏该控件，如果被固定将不会隐藏
     */
    public void tryHide() {
        if(this.getVisibility() == View.VISIBLE) {
            this.setVisibility(View.INVISIBLE);
        }
    }
    
}
